// pages/home/shangou/shangouOrder/shangouOrder.js
const service = require('../groupBuyService.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    groupBuyId: '',
    name: '',
    address: '',
    goodImgUrl: '/images/common/picDefault.png',
    goodName: '时尚黄色汽车儿童收纳凳',
    goodDetail: '时尚的很进本是新的，蛮喜欢的快点买...',
    expressType: 1,// 1包邮 2 不包邮
    expressPrice: 0,
    totalPrice: 0,
    addressData: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options)
    this.setData({
      groupBuyId: options.groupBuyId
    })
    if (options.groupBuyId) {
      this.setData({
        groupBuyId: options.groupBuyId
      })
    }
    var param = {
      userId: getApp().globalData.userId,
    }
    getApp().func.Get(false, getApp().api.getDefault_address, param,
      function (data) {
        if (data != '') {
          that.setData({
            addressData: data,
          })
        }
      },
      function (error) {
        wx.showToast({
          title: error.data.message,
          icon: 'none',
          duration: 2000,
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    if (!this.data.groupBuyId) {
      return;
    }
    service.orderDetail(this.data.groupBuyId, function (data) {
      if (data.groupBuyGoodsVo) {
        that.setData({
          goodImgUrl: data.groupBuyGoodsVo.goodsUrl,
          goodName: data.groupBuyGoodsVo.goodsName,
          goodDetail: data.groupBuyGoodsVo.description,
          totalPrice: data.groupBuyGoodsVo.integral,
          expressPrice: data.groupBuyGoodsVo.postage
        })
      }
    });
  },

  /**
   * 选择地址
   */
  chooseAddress: function (e) {
    wx.navigateTo({
      url: '../../../../pages/addProduct/myAddress/myAddres',
    })
  },

  /**
   * 提交订单
   */
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e)
    // 获取表单id
    let formId = e.detail.formId;
    getApp().saveUserFormId(formId,3);

    var slef = this;
    if (!this.data.groupBuyId) {
      return;
    }
    if (!this.data.addressData) {
      wx.showToast({
        title: '请选择地址',
        duration: 2000,
        icon: 'none'
      })
      return;
    }
    service.submitOrder(this.data.groupBuyId, this.data.addressData.id, function (data) {
      if (data.groupBuyUserId == "" || data.groupBuyId == ""){
        wx.showToast({
          title: "这个团火热的挤爆了，去别的团看看吧",
          icon: 'none'
        })
        return
      }
      wx.reLaunch({
        url: '../gBDetail/gBDetail?gBId=' + data.groupBuyId + "&gBUId=" + data.groupBuyUserId,
      })
    }, function (error) {
      wx.showToast({
        title: error.message,
        icon: 'none'
      })
    });
  },

})